package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.PromotionStatus;
import com.cloudfun.campusshare.common.constant.RoleType;
import com.cloudfun.campusshare.common.model.dto.*;
import com.cloudfun.campusshare.common.model.vo.PromotionDetailVO;
import com.cloudfun.campusshare.common.model.vo.PromotionListVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.PromotionService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by CodeGenerator on 2020/06/06
 */
@Slf4j
@RestController
@AuthRequired
@RequestMapping("/v1/Promotion")
@Api(value = "优惠活动模块", description = "优惠活动模块")
public class PromotionController {

    @Autowired
    private PromotionService promotionService;

    @PutMapping("/samePeriodList")
    @ApiOperation("同一时间段活动列表")
    public BaseResult<List<PromotionListVO>> samePeriodPromotionList(@RequestBody SamePeriodPromotionSearchDTO searchDTO) {
        List<PromotionListVO> promotionList = promotionService.samePeriodPromotionList(searchDTO);
        return BaseResultFactory.produceResult(Code.SUCCESS, promotionList);
    }

    @PostMapping("/coupon")
    @ApiOperation(value = "创建代金券活动")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    public BaseResult<?> creatCouponPromotion(@ApiParam(value = "createDTO", required = true) @Valid @RequestBody CouponPromotionCreateDTO createDTO) {
        try {
            promotionService.create(createDTO);
        } catch (Exception e) {
            return BaseResultFactory.produceEmptyResult(Code.ERROR,e.getMessage());
        }
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @PostMapping("/discount")
    @ApiOperation(value = "创建折扣活动")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    public BaseResult<?> creatDiscountPromotion(@ApiParam(value = "createDTO", required = true) @Valid @RequestBody DiscountPromotionCreateDTO createDTO) {
        try {
            promotionService.create(createDTO);
        } catch (Exception e) {
            return BaseResultFactory.produceEmptyResult(Code.ERROR,e.getMessage());
        }
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @PostMapping("/rebate")
    @ApiOperation(value = "创建满减活动")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    public BaseResult<?> rebateDiscountPromotion(@ApiParam(value = "createDTO", required = true) @Valid @RequestBody RebatePromotionCreateDTO createDTO) {
        try {
            promotionService.create(createDTO);
        } catch (Exception e) {
            return BaseResultFactory.produceEmptyResult(Code.ERROR,e.getMessage());
        }
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @PutMapping("/list")
    @ApiOperation("分页列表")
    BaseResult<PageResult<PromotionListVO>> search(Pageable pageable, @RequestBody PromotionSearchDTO searchDTO) {
        return BaseResultFactory.produceResult(Code.SUCCESS, promotionService.search(searchDTO, pageable));
    }

    @PutMapping("/onlineList")
    @ApiOperation("洗衣点上线活动列表")
    @AuthRequired(role = RoleType.CUSTOMER)
    BaseResult<List<PromotionDetailVO>> onlineList(@RequestBody PromotionSearchDTO searchDTO) {
        if (StringUtils.isBlank(searchDTO.getWashPointId())) {
            return BaseResultFactory.produceResult(Code.SUCCESS, new ArrayList<>());
        }
        searchDTO.setStatus(PromotionStatus.ONLINE);
        return BaseResultFactory.produceResult(Code.SUCCESS, promotionService.list(searchDTO));
    }

    @DeleteMapping
    @ApiOperation("删除")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    BaseResult<?> delete(@RequestParam String id) {
        promotionService.delete(id);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @PutMapping("/offline")
    @ApiOperation("下线")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    BaseResult<?> offline(@RequestParam String id) {
        promotionService.offline(Lists.newArrayList(id));
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @PutMapping("/offline/batch")
    @ApiOperation("批量下线")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    BaseResult<?> batchDelete(@ApiParam(value = "BatchOperateDTO", required = true) @RequestBody BatchOperateDTO<PromotionSearchDTO> batchOperateDTO) {
        if (CollectionUtils.isEmpty(batchOperateDTO.getIds()) && null == batchOperateDTO.getSearchData() && !BooleanUtils.isTrue(batchOperateDTO.getAll())) {
            return BaseResultFactory.produceEmptyResult(Code.PARAMETER_ERROR, "参数错误");
        }
        if (!CollectionUtils.isEmpty(batchOperateDTO.getIds())) {
            promotionService.offline(batchOperateDTO.getIds());
        } else if (null != batchOperateDTO.getSearchData()) {
            promotionService.offline(batchOperateDTO.getSearchData());
        } else {
            promotionService.offlineAll();
        }
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @GetMapping("/detail")
    @ApiOperation("详情")
    public BaseResult<PromotionDetailVO> samePeriodPromotionList(@RequestParam String id) {
        PromotionDetailVO detail = promotionService.detail(id);
        return BaseResultFactory.produceResult(Code.SUCCESS, detail);
    }

    @PutMapping
    @ApiOperation("修改")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    public BaseResult<?> modify(@RequestBody PromotionModifyDTO modifyDTO) {
        promotionService.modify(modifyDTO);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @GetMapping("/drawCoupon")
    @ApiOperation("领取代金券")
    @AuthRequired(role = RoleType.CUSTOMER)
    BaseResult<?> drawCoupon(@RequestParam String id) {
        promotionService.drawCoupon(id);
        return BaseResultFactory.produceSuccess();
    }

}